package com.devicemantra.coordinate.ds.tests;
import com.devicemantra.coordinate.dataobjects.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.devicemantra.coordinate.dataobjects.CDepartmentFactory;
import com.devicemantra.coordinate.exceptions.dataobjects.CDatabaseException;
import com.devicemantra.coordinate.exceptions.operations.CDepartmentDoesNotExistsException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@SuppressWarnings("serial")
public class DeleteDepartment extends HttpServlet 
{
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException 
	{
		resp.setContentType("text/html");
		/* Check whether a department exists with the same name */
		if(CDepartmentFactory.checkDepartmentExists(req.getParameter("dept_name")))
		{
			resp.getOutputStream().println("[Department Check failed] <BR> ");
		}
		else
		{
			resp.getOutputStream().println("[Department Check passed] <BR>");
		}
		
		
		boolean lIsDeleted = false;
		try {
			CDepartmentFactory.deleteDepartment((req.getParameter("dept_name")));
		} catch (CDepartmentDoesNotExistsException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (CDatabaseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if( lIsDeleted == true)
		{
			resp.getOutputStream().print("Department Deleted <BR>");
		}
		else
		{
			resp.getOutputStream().print("Department DOES NOT EXIST <BR> ");
		}
		
	}
}
